package customer.zspt.cn.customer.sqlites.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import customer.zspt.cn.customer.bean.ChatMsgDao;
import customer.zspt.cn.customer.constant.chatMsgFieldName;
import customer.zspt.cn.customer.sqlites.chatMsgSqliteHelpter;
/**
 * Created by Administrator on 2018/8/20 0020.
 */

public class chatMsgSqliteDao {
    public  chatMsgSqliteHelpter chatMsgSqliteHelpter = null;
    public static chatMsgSqliteDao msgSqliteDao = null;
    private Context cxt;
    //查询到的聊天记录集合
    private List<ChatMsgDao> msgDaoList ;
    public chatMsgSqliteDao(Context context){
        chatMsgSqliteHelpter = new chatMsgSqliteHelpter(context);
        cxt = context;
        msgDaoList = new ArrayList<>();
    }
    /**提供一个实例*/
    public static chatMsgSqliteDao getInstance(Context cxt){
        if(msgSqliteDao == null){
            msgSqliteDao = new chatMsgSqliteDao(cxt);
        }
        return msgSqliteDao;
    }
    /**更新数据库数据（增删改查）*/
    public void Update(String sql){
        SQLiteDatabase db = chatMsgSqliteHelpter.getWritableDatabase();
        db.execSQL(sql);
        Log.e("sql",sql);
        db.close();
    }
    /**查询数据(分页查询)*/
    public List<ChatMsgDao> query(String table,int start,int end){
        //清空数组
        msgDaoList.removeAll(msgDaoList);
        SQLiteDatabase db = chatMsgSqliteHelpter.getWritableDatabase();
        Cursor cursor = db.query(table,null,null,null,null,null,"_id desc",start+","+end);
        while (cursor.moveToNext()){
            ChatMsgDao msgDao = new ChatMsgDao();
            msgDao.setAvater(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.AVATER)));
            msgDao.setMsgType(cursor.getInt(cursor.getColumnIndex(chatMsgFieldName.MSGTYPE)));
            msgDao.setChatContent(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.CHATCONTENT)));
            msgDao.setSendOrfrom(cursor.getInt(cursor.getColumnIndex(chatMsgFieldName.SENDORFROM)));
            msgDao.setUserName(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.USERNAME)));
            msgDao.setTempTime(cursor.getLong(cursor.getColumnIndex(chatMsgFieldName.TEMPTIME)));
            msgDao.setGROUPAVATAR(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.GROUPAVATAR)));
            msgDao.setGROUPNAME(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.GROUPNAME)));
            msgDao.setCHATOBJAVATER(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.CHATOBJAVATER)));
            msgDao.setCHATOBJNAME(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.CHATOBJNAME)));
            msgDaoList.add(msgDao);
        }
        cursor.close();
        db.close();
        return msgDaoList;
    }
    /**增加表*/
    public void addNewTable(String tableName,String UpdateSql){
        SQLiteDatabase db = chatMsgSqliteHelpter.getWritableDatabase();
        String sql = " CREATE TABLE IF NOT EXISTS  "+tableName +"( _id INTEGER PRIMARY KEY " +
                "," +chatMsgFieldName.AVATER+" varchar(50)"
                +","+chatMsgFieldName.CHATCONTENT+" varchar(100)"+
                ","+chatMsgFieldName.SENDORFROM +" int "+
                ","+chatMsgFieldName.TEMPTIME +" long "+
                ","+chatMsgFieldName.USERNAME+" varchar(10)"+
                ","+chatMsgFieldName.READORUNREAD +" int "+
                ","+chatMsgFieldName.MSGTYPE +" int "+
                ","+chatMsgFieldName.GROUPNAME +" varchar(10) "+
                ","+chatMsgFieldName.GROUPAVATAR +" varchar(50) "+
                ","+chatMsgFieldName.CHATOBJAVATER +" varchar(50) "+
                ","+chatMsgFieldName.CHATOBJNAME +" varchar(10) "+
                ","+chatMsgFieldName.DATETIME +" varchar(22) "+
                ");";
        db.execSQL(sql);
        if(!TextUtils.isEmpty(UpdateSql)){
            db.execSQL(UpdateSql);
            Log.e("add",UpdateSql);
        }
        db.close();
    }
    //删除数据库
    public boolean deleteDatabase(Context cxt,String sqlName){
        return cxt.deleteDatabase(sqlName);
    }
    /**查询数据(分页查询)*/
    public List<ChatMsgDao> queryOnlyOneData(String table,String datetime){
        //清空数组
        msgDaoList.removeAll(msgDaoList);
        SQLiteDatabase db = chatMsgSqliteHelpter.getWritableDatabase();
        Cursor cursor = db.query(table,null,"datatime = ?",new String[]{datetime},null,null,null);
        while (cursor.moveToNext()){
            ChatMsgDao msgDao = new ChatMsgDao();
            msgDao.setAvater(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.AVATER)));
            msgDao.setMsgType(cursor.getInt(cursor.getColumnIndex(chatMsgFieldName.MSGTYPE)));
            msgDao.setChatContent(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.CHATCONTENT)));
            msgDao.setSendOrfrom(cursor.getInt(cursor.getColumnIndex(chatMsgFieldName.SENDORFROM)));
            msgDao.setUserName(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.USERNAME)));
            msgDao.setTempTime(cursor.getLong(cursor.getColumnIndex(chatMsgFieldName.TEMPTIME)));
            msgDao.setGROUPAVATAR(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.GROUPAVATAR)));
            msgDao.setGROUPNAME(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.GROUPNAME)));
            msgDao.setCHATOBJAVATER(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.CHATOBJAVATER)));
            msgDao.setCHATOBJNAME(cursor.getString(cursor.getColumnIndex(chatMsgFieldName.CHATOBJNAME)));
            msgDaoList.add(msgDao);
        }
        cursor.close();
        db.close();
        return msgDaoList;
    }
}
